import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_math_fork/flutter_math.dart';
import 'package:get/get.dart';
import 'package:http/http.dart' as http;
import 'package:math2/controller/TexController.dart';
import 'package:math2/pages/answer/question_input.dart';

class AnswerPage extends GetView<TexController> {
  const AnswerPage({super.key});

  @override
  Widget build(BuildContext context) {
    Get.put(TexController());
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          '问题解答',
          style: TextStyle(fontSize: 12),
        ),
      ),
      body: Column(
        // mainAxisSize: MainAxisSize.max,
        // crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          const Padding(
            padding: EdgeInsets.all(10),
            child: QuestionInput(),
          ),
          Expanded(
            flex: 1,
            child: Padding(
              padding: const EdgeInsets.all(10),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  const Padding(
                    padding: EdgeInsets.all(10),
                    child: Text(
                      "TeX方程式",
                      style: TextStyle(fontSize: 12),
                    ),
                  ),
                  Container(
                      height: MediaQuery.of(context).size.height / 8,
                      decoration: BoxDecoration(
                          border: Border.all(width: 1),
                          borderRadius: BorderRadius.circular(5)),
                      alignment: Alignment.topLeft,
                      padding: EdgeInsets.all(10),
                      child: Obx(() => SelectableMath.tex(
                            // '${super.contoller.tex}',
                            super.controller.tex.value,
                            textStyle: const TextStyle(fontSize: 12),
                          ))),
                  ElevatedButton(
                      onPressed: () async {
                        // https://console.bce.baidu.com/qianfan/ais/console/applicationConsole/application
                        var clientId = 'N7fCt4NfMeSy7EUhQirfWerU';
                        var clientSecret = 'ZrnEUIetNllLozyCfUFRQLsh2ayRzxct';
                        var url = Uri.parse('https://aip.baidubce.com/oauth/2.0/token?client_id=$clientId&client_secret=$clientSecret&grant_type=client_credentials');
                      
                        var response = await http.post(url);
                        print('Response status: ${response.statusCode}');
                        print('Response body: ${response.body}');

                        if(response.statusCode != 200) {
                          return;
                        }

                        var accessToken = json.decode(response.body)['access_token'];
                        var body = {
                          'messages': [
                            {'role': 'user', 'content': '我了个去'}
                          ]
                        };

                        url = Uri.parse('https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant?access_token=$accessToken');
                        response = await http.post(url, body: json.encode(body));
                        print('Response status: ${response.statusCode}');
                        print('Response body: ${response.body}');
                      },
                      child: const Text(
                        "解答",
                        style: TextStyle(fontSize: 12),
                      )),
                  Expanded(
                      child: Container(
                    decoration: BoxDecoration(
                        border: Border.all(width: 1),
                        borderRadius: BorderRadius.circular(5)),
                    alignment: Alignment.topLeft,
                    padding: const EdgeInsets.all(10),
                    child: SelectableMath.tex(
                      'x=x+1',
                      textStyle: const TextStyle(fontSize: 12),
                    ),
                  ))
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
